package com.beerandjesus.sportofkings.handicapper.migration.hibernate;

import org.hibernate.Query;
import org.hibernate.Session;

import java.util.Iterator;

public class HibernateCopier {

    public <T> void copy(Class<T> entityClass, Session from, Session to) {
        Query query = from.createQuery("from " + entityClass.getCanonicalName());
        Iterator list = query.iterate();
        int i = 0;
        while (list.hasNext()) {
            T entity = (T) list.next();
            to.save(entity);
            if (i++ == 100) {
                to.flush();
            }
        }
    }

}